package com.example.springbootgraduation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootgraduation.entity.HealthClock;
import com.example.springbootgraduation.service.HealthClockService;
import com.example.springbootgraduation.vo.DataView;
import com.example.springbootgraduation.vo.HealthClockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//健康打卡
@Controller
public class HealthClockController {

    @Autowired
    private HealthClockService healthClockService;//集成了MybatisPlus所有的增删改查操作

    /**
     * 健康打卡跳转页面
     */
    @RequestMapping("/toHealthClock")
    public String toHealthClock(){
        return "admin/healthclock";
    }

    /**
     * 查询所有健康打卡记录，带有模糊查询条件 带有分页
     * @param healthClockVo
     * @return
     */
    @RequestMapping("/listHealthClock")
    @ResponseBody
    public DataView listHealthClock(HealthClockVo healthClockVo){//HealthClockVo实现分页效果需要
        //查询所有带有模糊查询条件 带有分页
        //创建分页查询对象
        IPage<HealthClock> page = new Page<>(healthClockVo.getPage(),healthClockVo.getLimit());
        //模糊查询
        QueryWrapper<HealthClock> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(healthClockVo.getUsername() !=null,"username",healthClockVo.getUsername());//用户名模糊查询
//        queryWrapper.eq(healthClockVo.getPhone() !=null,"phone",healthClockVo.getPhone());//手机号模糊查询
        queryWrapper.like(!(healthClockVo.getUsername() ==null),"username",healthClockVo.getUsername());//用户名模糊查询
        queryWrapper.like(!(healthClockVo.getPhone() ==null),"phone",healthClockVo.getPhone());//手机号模糊查询
        healthClockService.page(page,queryWrapper);
        return new DataView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加或修改健康打卡记录数据
     */
    @RequestMapping("/addHealthClock")
    @ResponseBody
    public DataView addHealthClock(HealthClock healthClock){
        boolean b = healthClockService.saveOrUpdate(healthClock);//添加或者修改
        DataView dataView = new DataView();
        if (b){
            dataView.setCode(200);
            dataView.setMsg("添加成功!");
        }else {
            dataView.setCode(100);
            dataView.setMsg("添加失败！");
        }
        return dataView;
    }

    /**
     * 删除健康打卡数据
     */
    @RequestMapping("/deleteHealthClockById")
    @ResponseBody
    public DataView deleteHealthClockById(Integer id){
        healthClockService.removeById(id);
        DataView dataView=new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除成功！");
        return dataView;
    }
}
